<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
    public $layout = '//layouts/main';

    public $connection = null;

    public $menus = null;

    public $data = array();

    public function init()
    {
        $this->connection = Yii::app()->db;
    }

    public function filters()
    {
        return array(
            array(
                'application.filters.Sessionfilter',
            )
        );
    }

    public function run($actionID)
    {
        $transaction = $this->connection->beginTransaction();
        try {

            if (($action = $this->createAction($actionID)) !== null) {
                if (($parent = $this->getModule()) === null)
                    $parent = Yii::app();
                if ($parent->beforeControllerAction($this, $action)) {
                    $this->runActionWithFilters($action, $this->filters());
                    $parent->afterControllerAction($this, $action);
                }
            } else
                $this->missingAction($actionID);

            $transaction->commit();
        } catch (Exception $e) {
            print_r($e);
            $transaction->rollback();
        }
    }

    protected function getLastUserInfo()
    {
        $user_id = Yii::app()->user->user_id;
        $userinfo = $this->connection->createCommand("select * from sys_user where user_id = :user_id")->bindParam(':user_id', $user_id, PDO::PARAM_INT)
            ->queryRow();
        return $userinfo;
    }

    protected function beforeRender($view)
    {
        //获取memu,menuitem
        $menus = Yii::app()->db->createCommand("select * from sys_menu where type = 'menu' and disabled = 0 and
 menushow = 1 ORDER BY seq desc")
            ->queryAll();
        foreach ($menus as &$menu) {
            $menuitems = Yii::app()->db->createCommand("select * from sys_menu where type = 'menuitem'
 and parent_id = :parent_id and disabled = 0 and menushow = 1 ORDER BY seq desc")
                ->bindParam(':parent_id', $menu['menu_id'], PDO::PARAM_STR)
                ->queryAll();
            $menu['items'] = $menuitems;
        }
        $this->menus = $menus;
        return true;
    }


}